<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tmporder_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
        $this->config->load('userset');//用户积分
	}

    //找出最新商品订单
    public function get_goods_order_by_good_id_news($good_id,$uid)
    {
        $query = $this->db->select_max('order_add_time')->get_where('tmp_orders', array('good_id'=>$good_id, 'user_id'=>$uid));
        return $query->row_array();
    }

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_tmp_orders_by_id($id){
        $query = $this->db
            ->select('*')
            ->get_where('tmp_orders', array('id'=>$id));
        return $query->row_array();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改订单信息
     */
    public function sava_tmp_orders($id,$data){

        $this->db->where('id',$id);
        $this->db->update('tmp_orders', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增订单
     */
    public function add_tmp_orders($data){

        $this->db->insert('tmp_orders',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有订单
     */
    public function get_all_tmp_orders(){
        $this->db->select('*');
        $this->db->from('tmp_orders');
        $this->db->join('shop_users','tmp_orders.shop_id = shop_users.shop_id','left');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function sucsess_order($id){

        $data = $this->get_tmp_orders_by_id($id);

        $this->db->trans_start();
        // 更新订单状态为成功
        $this->db->set('order_status',2,false)->where('id',$id)->update('tmp_orders');
        $this->db->set('order_add_time',time(),false)->where('id',$id)->update('tmp_orders');
        // 商品库存减少
        $this->db->set('good_stock','good_stock-1',false)->where('good_id',$data['good_id'])->update('shop_goods');
        $this->db->set('good_orders','good_orders+1',false)->where('good_id',$data['good_id'])->update('shop_goods');
        // 用户积分增加+积分明细
        $this->db->set('user_credit','user_credit+'.$this->config->item('credit_jianlou'),false)->where('user_id',$data['user_id'])->update('users');
        $credit = array(
            "user_id"=>$data['user_id'],
            "credit_num"=>$this->config->item('credit_jianlou'),
            "credit_type"=>3,
            "credit_time"=>time(),
        );
        $this->db->insert('credits',$credit);
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
            return false;
        }else{
            return true;
        }
    }


    /**
     * 订单待确认
     * @param $data
     */
    public function wait_order($data,$type=0){

        $this->db->trans_start();
        if($type == 0){
            $this->db->insert('tmp_orders',$data);
            $id = $this->db->insert_id();
            $this->db->set('user_money','user_money-'.$data['order_price'],false)->where('user_id',$data['user_id'])->update('users');
            $money = array(
                "user_id"=>$data['user_id'],
                "money_num"=>$data['order_price'],
                "money_item"=>$data['good_id'],
                "money_type"=>2,
                "money_time"=>time(),
            );
            $this->db->insert('moneys',$money);
            $this->db->set('order_pay','3',false)->where('id',$id)->update('tmp_orders');
            $this->db->trans_complete();
            return $id;
        }
        if($type == 2){

            // 成功逻辑
            // 更新订单状态为成功
            $this->db->set('order_status',$type,false)->where('id',$data['id'])->update('tmp_orders');
            // 商品库存减少
            $this->db->set('good_stock','good_stock-1',false)->where('good_id',$data['good_id'])->update('shop_goods');
            // 订单数加一
            $this->db->set('good_orders','good_orders+1',false)->where('good_id',$data['good_id'])->update('shop_goods');
            // 用户积分增加+积分明细
            $this->db->set('user_credit','user_credit+'.$this->config->item('credit_jianlou'),false)->where('user_id',$data['user_id'])->update('users');
            $credit = array(
                "user_id"=>$data['user_id'],
                "credit_num"=>$this->config->item('credit_jianlou'),
                "credit_type"=>3,
                "credit_time"=>time(),
            );
            $this->db->insert('credits',$credit);
            // 更新捡漏成功的时间
            $this->db->set('order_update_time',time(),false)->where('id',$data['id'])->update('tmp_orders');
            //商家操作确认
            $this->db->set('order_shop_ok',1,false)->where('id',$data['id'])->update('tmp_orders');
            $this->db->trans_complete();
        }
        if($type == 1){
            // 失败，回滚
            $this->db->set('order_status',$type,false)->where('id',$data['id'])->update('tmp_orders');
            $this->db->set('user_money','user_money+'.$data['order_price'],false)->where('user_id',$data['user_id'])->update('users');
            $money = array(
                "user_id"=>$data['user_id'],
                "money_num"=>$data['order_price'],
                "money_item"=>$data['good_id'],
                "money_type"=>1,
                "money_time"=>time(),
            );
            //商家操作确认
            $this->db->set('order_shop_ok',1,false)->where('id',$data['id'])->update('tmp_orders');
            $this->db->insert('moneys',$money);
            $this->db->trans_complete();
        }

		if ($this->db->trans_status() === FALSE)
		{
			return false;
		}else{
			return true;
		}  
		
	}

    /**
     * @return mixed
     * 查询最进成交的10条订单
     */
    public function get_ten_tmp_orders(){
        $field = "tmp_orders.user_id,users.user_avatar,users.user_name,users.user_uname,tmp_orders.order_update_time";
        $this->db->select($field);
        $this->db->from('tmp_orders');
        $this->db->join('users','tmp_orders.user_id = users.user_id','left');
        $this->db->where("tmp_orders.order_status",2);
        $this->db->limit(8);
        $this->db->order_by('tmp_orders.id',"DESC");
        // $this->db->join('shop_goods as g','o.good_id = g.good_id','left');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    /**
     * @return mixed
     * 查询最进成交的10条订单
     */
    public function get_ten_shop_goods(){
        $field = "shop_goods.shop_id,shop_goods.user_id,users.user_avatar,users.user_name,users.user_uname,shop_goods.good_add_time";
        $this->db->select($field);
        $this->db->from('shop_goods');
        $this->db->join('users','shop_goods.user_id = users.user_id','left');
        $this->db->limit(3);
        $this->db->order_by('shop_goods.good_id',"DESC");
        // $this->db->join('shop_goods as g','o.good_id = g.good_id','left');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

//后台查询所有的订单
    public function get_all_orders($start,$limit,$uid = 0){
        $this->db->select('*');
        $this->db->from('tmp_orders');
        if(!empty($uid)){
            $this->db->where("user_id",$uid);
        }
        $this->db->limit($limit,$start);
        $this->db->order_by('id','DESC');
        $query = $this->db->get();
        return $query->result_array();
            
    }

    public function get_recent_one_record($id='')
    {
        $field = "tmp_orders.user_id,users.user_avatar,users.user_name,users.user_uname,tmp_orders.order_update_time";
        $this->db->select($field);
        $this->db->from('tmp_orders');
        $this->db->join('users','tmp_orders.user_id = users.user_id','left');
        $this->db->where("tmp_orders.id",$id);
        $query = $this->db->get();
        return $query->row_array();
    }
    //查询已发货的订单
    public function get_delivers($start,$limit){
        $this->db->select('*')->from('tmp_orders')->where(array('order_status'=>2,'order_deliver'=>2))->limit($limit,$start)->order_by('id','DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
    //查询未发货的订单
    public function get_delivering($start,$limit){
        $this->db->select('*')->from('tmp_orders')->where(array('order_status'=>2,'order_deliver'=>1))->limit($limit,$start)->order_by('id','DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
    //查询所有需要未发货的订单的总数
    public function count_all_ing(){
        $this->db->select('id');
        $this->db->from('tmp_orders');
        $this->db->where(array('order_status'=>2,'order_deliver'=>1));
        $total=$this->db->count_all_results();
        return $total;
    }
    //查询所有已发货的订单的总数
    public function count_all_ings(){
        $this->db->select('id');
        $this->db->from('tmp_orders');
        $this->db->where(array('order_status'=>2,'order_deliver'=>2));
        $total=$this->db->count_all_results();
        return $total;
    }
    //商家端获取自己的订单
    public function get_tmp_orders($page=0,$limit=4,$shopid,$type=1000){
        $start = $page*$limit;
        $this->db->select('tmp_orders.*,shop_goods.good_title,shop_goods.good_cover');
        $this->db->from('tmp_orders');
        $this->db->join('shop_goods','tmp_orders.good_id = shop_goods.good_id','left');
        $this->db->where(array('tmp_orders.order_status'=>2,'tmp_orders.shop_id'=>$shopid));
        if($type<>1000){
            $this->db->where('order_deliver',$type);
        }
        $this->db->order_by('tmp_orders.id','DESC');
        $this->db->limit($limit,$start);
        $query = $this->db->get();
        return $query->result_array();

    }
    //商家端获取自己的所有订单
    public function get_tmp_orders_all($page=0,$limit=4,$shopid){
        $start = $page*$limit;
        $this->db->select('tmp_orders.*,shop_goods.good_title,shop_goods.good_cover');
        $this->db->from('tmp_orders');
        $this->db->join('shop_goods','tmp_orders.good_id = shop_goods.good_id','left');
        $this->db->where(array('tmp_orders.shop_id'=>$shopid));
        $this->db->order_by('tmp_orders.id','DESC');
        $this->db->limit($limit,$start);
        $query = $this->db->get();
        return $query->result_array();

    }
    //查询所有订单的总数
    public function get_tmp_orders_count_all($shopid,$type=1000){
        $this->db->select('id');
        $this->db->from('tmp_orders');
        if($type<>1000){
            $this->db->where('order_deliver',$type);
        }
        $this->db->where(array('shop_id'=>$shopid));
        $total=$this->db->count_all_results();
        return $total;
    }
    //查询所有已发货的订单的总数
    public function get_tmp_orders_count($shopid,$type=1000){
        $this->db->select('id');
        $this->db->from('tmp_orders');
        if($type<>1000){
            $this->db->where('order_deliver',$type);
        }
        $this->db->where(array('order_status'=>2,'shop_id'=>$shopid));
        $total=$this->db->count_all_results();
        return $total;
    }
    //删除失败订单
    public function del_order_by_id($id){
        $this->db->where('id',$id)->delete('tmp_orders');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * 个人某商品订单总金额
     * @param $uid
     * @param $good_id
     * @return mixed
     */
    public function personal_order_total_fee($uid,$good_id){
        $this->db->select("sum(order_price) as total_fee");
        $this->db->from('tmp_orders');
        $this->db->where("user_id",$uid);
        $this->db->where("good_id",$good_id);
        $this->db->where("order_status",2);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function personal_order_total_count($uid,$good_id){
        $this->db->select("id");
        $this->db->from('tmp_orders');
        $this->db->where("user_id",$uid);
        $this->db->where("good_id",$good_id);
        $this->db->where("order_status",2);
        $total=$this->db->count_all_results();
        return $total;
    }

    /**
     * 获取下级某商品订单
     * @param $good_id
     * @param $ids 下级数据
     */
    public function get_sub_good_order($good_id,$ids){
        $this->db->select("id");
        $this->db->from('tmp_orders');
        $this->db->where_in("user_id",$ids);
        $this->db->where("good_id",$good_id);
        $this->db->where("order_status",2);
        $total=$this->db->count_all_results();
        return $total;

    }

    public function get_orders_with_voucher_by_uid($uid){
        $this->db->select('tmp_orders.*,');
        $this->db->from('tmp_orders');
        $this->db->join('shop_goods','tmp_orders.good_id = shop_goods.good_id','left');
        $this->db->where(array('tmp_orders.order_status'=>2,'tmp_orders.user_id'=>$uid,'shop_goods.good_voucher'=>1));
        $this->db->order_by('tmp_orders.id','DESC');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function voucher_order($order){

        $this->db->insert('tmp_orders',$order);
        $id = $this->db->insert_id();
        // 商品库存减一
        $this->db->set('good_stock','good_stock-1',false)->where('good_id',$order['good_id'])->update('shop_goods');
        // 订单数加一
        $this->db->set('good_orders','good_orders+1',false)->where('good_id',$order['good_id'])->update('shop_goods');
        // 免单券状态改为已使用
        $this->db->set('voucher_lock',1,false)->where('good_id',$order['good_id'])->update('free_vouchers');
        return $id;
    }


    public function create_order($data){

        $this->db->insert('tmp_orders',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_tmp_orders_by_order_no($order_no){
        $query = $this->db
            ->select('*')
            ->get_where('tmp_orders', array('order_no'=>$order_no));
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function sava_tmp_orders_by_no($order_no,$data){

        $this->db->where('order_no',$order_no);
        $this->db->update('tmp_orders', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function get_all_orders_mobile($start,$limit,$uid = 0){
        $this->db->select('tmp_orders.*,shop_goods.good_title,shop_goods.good_category,shop_goods.good_price,shop_goods.good_memo');
        $this->db->from('tmp_orders');
        $this->db->join('shop_goods','tmp_orders.good_id = shop_goods.good_id','left');
        if(!empty($uid)){
            $this->db->where("tmp_orders.user_id",$uid);
        }
        $this->db->where("tmp_orders.order_status >",0);
        $this->db->limit($limit,$start);
        $this->db->order_by('tmp_orders.id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }

    }

    public function get_excel_orders_by_time($end,$start){
        $this->db->select('*');
        $this->db->from('tmp_orders');
        $this->db->join('users',' users.user_id= tmp_orders.user_id','left');
        if(!empty($start) && !empty($end)){
            $starts = strtotime($start);
            $ends = strtotime($end);
            $this->db->where('order_add_time >=',$starts);
            $this->db->where('order_add_time <=',$ends);
        }
        $this->db->order_by('id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

}
